package com.example.dp01.api;

import com.example.dp01.entity.Teacher;
import com.example.dp01.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {
    @Autowired
    private TeacherService teacherService;
    @GetMapping("/list")
    public List<Teacher> findAll(){

        List<Teacher> teachers= teacherService.getAll();
        return teachers;
    }
    @GetMapping("/{id}")
    public Teacher findById(@PathVariable Integer id) {
        if (id == null) {
            // 这里可以根据您的业务需求抛出自定义异常或者返回一个适当的响应
            throw new IllegalArgumentException("学生ID不能为空");
        }
        Teacher teacher = teacherService.findById(id);
        return teacher;
    }

    @PostMapping("/add")
    public Teacher add( Teacher teacher){
        return teacherService.add(teacher);
    }
    @PutMapping("/update")
    public Teacher update(Teacher teacher){
        return teacherService.update(teacher);
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        teacherService.delete(id);

    }

}
